#include <stdio.h>
#include "mythBaseServer.hh"
#define  MAX_ADDRESSES 1
int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_EVENTS);
	IPaddress addresses[MAX_ADDRESSES];
	mythVirtualSqlite* sqlite = mythVirtualSqlite::CreateNew("myth.db");
	mythBaseServer* baseserver = mythBaseServer::CreateNew(5830);
	if (argc > 1){
		SDL_Log("%s\n", argv[1]);
		string tmpip = argv[1];
		mythVirtualSqlite::GetInstance()->doSQL("update server set internalip = '" + tmpip + "',externalip = '" + tmpip + "' where serverid = 1");
	}
	else{
		int count = SDLNet_GetLocalAddresses(addresses, MAX_ADDRESSES);
		if (count > 0){
			SDL_Log("%d: %d.%d.%d.%d\n", 1,
				(addresses[0].host >> 0) & 0xFF,
				(addresses[0].host >> 8) & 0xFF,
				(addresses[0].host >> 16) & 0xFF,
				(addresses[0].host >> 24) & 0xFF);
			string tmpip = SDLNet_ResolveIP(&addresses[0]);
			mythVirtualSqlite::GetInstance()->doSQL("update server set internalip = '" + tmpip + "',externalip = '" + tmpip + "' where serverid = 1");
		}
	}
	printf("StartListen...\n");
	baseserver->StartServer();
	sqlite->close();
	return 0;
}